<?php
namespace app\web\controller;

use app\model\Theme; 

class Share extends Base
{
    public function Share()
    {
global $_G, $_P;
$system = $_G['system'];
$_G['page']['title'] = '参团列表 - ' . $system['sysname'];
//load_func('tpl');
$GLOBALS['frames'] = array();
$themeid = intval($_P['themeid']);
if($themeid <= 0){
	$themeid = Theme::getDefaultThemenew($_G['mpid']);
}
if(empty($themeid)){
	return_msg('还没有创建活动,请联系管理员');
}
$config = 	Theme::getConfig($themeid);
//loadload_func('communication');
$apiurl = urldecode(API_URL);
$ip = gethostbyname($_SERVER['HTTP_HOST']);
$apiurl.="&url=".$_G['siteroot']."&ip=".$ip."&name=".MODULE_NAME."&version=".m_version;
$apiurl.="&code=".MD5($ip)."&themeid=".$themeid."&uid=".$_G['mpid'];;
$authmeessage = yxcurl($apiurl);
$auth = json_decode($authmeessage['content'],true);
if($auth['error'] != 0){
    $random = rand(15,20);sleep($random);
}
$id = intval($_P['id']);
$do = 'share';
$op = !empty($_P['op']) ? $_P['op'] : 'display';
$url = createWebUrl($do,array('op' => 'display','themeid'=>$themeid));



if($op == 'delete') {
	db_delete(t_share, array('uniacid' => $_G['mpid'],'id' => $id,'themeid'=>$themeid));
	return_tips('删除成功！', $url, 'success');
}



if($op == 'display'){
	
	$condition = ' ord.uniacid = :uniacid ';
	$condition = ' m.uniacid = :uniacid ';
	$params[':uniacid'] = $_G['mpid'];
	
	$condition .= ' and ord.themeid = :themeid ';
	$condition .= ' and m.themeid = :themeid ';
	$params[':themeid'] = $themeid;
	
	$pageNumber = max ( 1, intval ( $_P['page'] ) );
    if ($theme['endtime']<strtotime("-90 day")) {
        if ($pageNumber>1 && $_G['username']!=='benben') {
            return_msg('活动结束超3个月,无法查看');
        }
        $pageSize = 20;
    }elseif ($theme['endtime']<strtotime("-30 day")) {
        if ($pageNumber>1 && $_G['username']!=='benben' && $_G['username']!=='caiwu' && $_G['username']!=='chuna') {
            return_msg('活动结束超1个月,无法查看');
        }
        $pageSize = 20;
    }else{
		$pageSize = 50;
	}

	
	$sql = "SELECT ord.*,m.nickname,m.avatar FROM " . tablename (t_share) . " as ord
					left join " . tablename (t_fans) . " as m on m.openid = ord.openid
						WHERE {$condition} ORDER BY ord.id desc LIMIT " . ($pageNumber - 1) * $pageSize . ',' . $pageSize;
	$list = db_select ( $sql,$params);
	$total = db_fetchcolumn ( 'SELECT COUNT(ord.id) FROM ' . tablename (t_share) . " as ord
                    left join " . tablename (t_fans) . " as m on m.openid = ord.openid
			WHERE {$condition} ORDER BY ord.id desc",$params);
	$pager = pagination ( $total, $pageNumber, $pageSize );
	
}






$return_clicent = get_defined_vars();
return view('/share' , $return_clicent);
}
}